package com.example.lt.utils;

import cn.hutool.core.lang.UUID;

import java.util.Random;

/**
 * @Author 小涛Tao
 * @Date: 2024/06/07/21:32
 * version 1.0 注释：随机验证码获取工具类
 **/
public class RandomUtils {
    public static String code(){
        String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; // 可以包含的字符集合
        int length = 5; // 生成的字符串长度为5个字符
        StringBuilder randomString = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            int randomIndex = random.nextInt(characters.length());
            char randomChar = characters.charAt(randomIndex);
            randomString.append(randomChar);
        }
        return randomString.toString();
    }

    /**
     * 获取随机的文件名
     * @param fileName
     * @return
     */
    public static String randomName(String fileName) {
        int length = fileName.length();
        String newName = fileName.substring(length - 4);
        String randomName = UUID.randomUUID().toString().substring(0, 9) + newName; // 随机数取8位
        return randomName;
    }
}
